package vn.co.thn.android.videohot.adapter;

import java.util.List;

import vn.amobi.util.ads.AmobiAdView;
import vn.amobi.util.ads.AmobiAdView.WidgetSize;
import vn.co.thn.android.videohot.R;
import vn.co.thn.android.videohot.VHApp;
import vn.co.thn.android.videohot.VHConstants;
import vn.co.thn.android.videohot.activity.VHVideoDetailActivity;
import vn.co.thn.android.videohot.common.VHCommonAdapter;
import vn.co.thn.android.videohot.data.VHVideoItemData;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


public class VHVideoListAdapter extends VHCommonAdapter<VHVideoItemData> implements OnClickListener{

	public VHVideoListAdapter(Context context, List<VHVideoItemData> objects) {
		super(context, R.layout.vh_video_item, objects);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		ViewHolder viewHolder;
		if (view == null) {
			view = mInflater.inflate(R.layout.vh_video_item, parent,false);
			viewHolder = new ViewHolder();
			viewHolder.title = (TextView) view.findViewById(R.id.title);
			viewHolder.thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
			viewHolder.thumbnail.setTag(getItem(position).getEmbed_code());
			viewHolder.adView=(AmobiAdView) view.findViewById(R.id.main_menu_adView);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}
		viewHolder.thumbnail.setOnClickListener(this);
		viewHolder.thumbnail.setTag(R.id.POSITION, position);
		viewHolder.title.setText(getItem(position).getTitle());
		
		if(position==0){
			if (viewHolder.adView != null) {
				// adview.setEventListener(this);
				viewHolder.adView.loadAd(WidgetSize.SMALL);
				// adView.scheduleRefresh(30000);
				viewHolder.adView.setHideAfterClick(false);
				// adview.stopRefresh();
			}
			viewHolder.adView.setVisibility(View.VISIBLE);
		}else{
			viewHolder.adView.setVisibility(View.GONE);
		}
		VHApp.Load_Image(getItem(position).getImage_Url(), viewHolder.thumbnail);
		return view;
	}

	private class ViewHolder {
		TextView title;
		ImageView thumbnail;
		AmobiAdView adView;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.thumbnail:
			VHVideoItemData video = getItem((Integer) v.getTag(R.id.POSITION));
			getContext().startActivity(new Intent(getContext(), VHVideoDetailActivity.class).putExtra(VHConstants.EXTRA_VIDEO, video));
			break;

		default:
			break;
		}
	}


}
